﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ProductCategories
    {
        public void Insert(tbl_ProductCategory productCategory)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductCategory newproductCategory = new tbl_ProductCategory()
                    {
                        Status=productCategory.Status,
                        Root=productCategory.Root,
                        ParentCategoryNo=productCategory.ParentCategoryNo,
                        Name=productCategory.Name,
                        Link=productCategory.Link,
                        Description=productCategory.Description,
                        CreatedUser=productCategory.CreatedUser,
                        CreatedDate=productCategory.CreatedDate,
                        CategoryImg=productCategory.CategoryImg,
                        CategoryCode=productCategory.CategoryCode,
                        BreadCrum=productCategory.BreadCrum
                    };
                    contextdb.tbl_ProductCategories.InsertOnSubmit(newproductCategory);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductCategory productCategory)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproductCategory = contextdb.tbl_ProductCategories.SingleOrDefault(u => u.CategoryNo == productCategory.CategoryNo);
                try
                {
                    if (updateproductCategory != null)
                    {
                        updateproductCategory.Status=productCategory.Status;
                        updateproductCategory.Root=productCategory.Root;
                        updateproductCategory.ParentCategoryNo=productCategory.ParentCategoryNo;
                        updateproductCategory.Name=productCategory.Name;
                        updateproductCategory.Link=productCategory.Link;
                        updateproductCategory.Description=productCategory.Description;
                        updateproductCategory.CreatedUser=productCategory.CreatedUser;
                        updateproductCategory.CreatedDate=productCategory.CreatedDate;
                        updateproductCategory.CategoryImg=productCategory.CategoryImg;
                        updateproductCategory.CategoryCode=productCategory.CategoryCode;
                        updateproductCategory.BreadCrum = productCategory.BreadCrum;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int productCategoryNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductCategory = contextdb.tbl_ProductCategories.SingleOrDefault(u => u.CategoryNo == productCategoryNo);
                try
                {
                    if (deleteproductCategory != null)
                    {
                        contextdb.tbl_ProductCategories.DeleteOnSubmit(deleteproductCategory);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int productCategoryNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductCategory = contextdb.tbl_ProductCategories.SingleOrDefault(u => u.CategoryNo == productCategoryNo);
                try
                {
                    if (deleteproductCategory != null)
                    {
                        deleteproductCategory.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}
